<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceUpdateToDo extends core {

    public function process() {
        $data = array();
        $todo_id = intval($_POST['todo_id']);
        $user_id = intval($_POST['user_id']);
        if($_POST['todo_item']===""){
             $this->_JSONResponse(array('error' => 1, 'msg' => "Please enter task description."));
        }
        if($_POST['start_time']==="" || $_POST['start_time'] == "Start Time*"){
             $this->_JSONResponse(array('error' => 1, 'msg' => "Please enter task’s start time."));
        }
        if($_POST['end_time']==="" || $_POST['end_time'] == "End Time*"){
             $this->_JSONResponse(array('error' => 1, 'msg' => "Please enter task’s end time."));
        }
        $current_date = date('Y-m-d',time());
        if(strtotime($_POST['todo_date'])< strtotime($current_date)){
             $this->_JSONResponse(array('error' => 1, 'msg' => 'Start date cannot be sooner than current date!'));
        }
        if(strtotime($_POST['todo_date'] . " " . $_POST['start_time'])< time()){
             $this->_JSONResponse(array('error' => 1, 'msg' => 'Start time cannot be sooner than current time!'));
        }
        if(strtotime($_POST['todo_date'] . " " . $_POST['start_time']) > strtotime($_POST['todo_date'] . " " . $_POST['end_time'])){
             $this->_JSONResponse(array('error' => 1, 'msg' => 'End time cannot be sooner than start time!'));
        }
        $data['todo_item'] = strip_tags(trim($_POST['todo_item']));      
        $start_date = date('Y-m-d H:i:s', strtotime($_POST['todo_date']));
        $data['start_date'] = $start_date;
        
        $start_time = date('Y-m-d H:i:s', strtotime($_POST['todo_date'] . " " . $_POST['start_time']));
        $data['start_time'] = $start_time;
        $end_time = date('Y-m-d H:i:s', strtotime($_POST['todo_date'] . " " . $_POST['end_time']));
        $data['end_time'] = $end_time;
        $data['user_id'] = $user_id;
        $data['timestamp'] = date('Y-m-d H:i:s', time());
        
        $fields = array();
        $values = array();
        $this->_preDataForInsert($data, $fields, $values);
        $fields = implode(',', $fields);
        $values = implode(',', $values);
        
        if ($todo_id > 0) {
            $todo_id = $this->db->escape($todo_id);
            $result_todo = $this->db->get_row("SELECT * FROM todo_lists WHERE todo_id=$todo_id AND user_id=$user_id");
           
            if ($result_todo) {
               
                $todo_item = $this->db->escape($data['todo_item']); 
                $start_date= $data['start_date'];
                $start_time = $data['start_time'];
                $end_time = $data['end_time'];              
                $query = "UPDATE `todo_lists` SET `todo_item`='$todo_item',`start_date`='$start_date',`start_time`='$start_time',`end_time`= '$end_time',
                        `timestamp`='".$data['timestamp']."' WHERE `todo_id`= '$todo_id'";
               
                if ($this->db->query($query)===false) {

                    $this->_JSONResponse(array('error' => 1, 'msg' => 'Oops, something went wrong. Please try again'));
                } else {
                    $this->_JSONResponse(array('error' => 0, 'msg' => 'Changes have been saved'));
                }
            } else {
                $this->_JSONResponse(array('error' => 1, 'msg' => 'Task does not exist'));
            }
        } else {
            if ($this->db->query("INSERT INTO `todo_lists` ({$fields}) VALUES ({$values});")===false) {

                $this->_JSONResponse(array('error' => 1, 'msg' => 'Oops, something went wrong. Please try again'));
            } else {
                $this->_JSONResponse(array('error' => 0, 'msg' => 'Task has been added'));
            }
        }
    }

}

$processor = new webServiceUpdateToDo();
$processor->process();
?>